// pages/sys/dept/form/index.js
import {
  request
} from '../../../../request';
import WxValidate from "../../../../utils/WxValidate";
Page({

  /**
   * 页面的初始数据
   */
  data: {
    deptIndex: null,
    dept: [],
    status: true,
    form: {
      deptName: null,
      email: null,
      leader: null,
      orderNum: 0,
      parentId: null,
      phone: null,
      status: "0",
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    //获取部门
    request("/system/dept/list", "GET").then(resopnse => {
      this.setData({
        dept: resopnse.data
      });
    })
    this.initValidate();
    //修改form
    if (options.id != undefined) {
      this.loadFormData(options.id);
    }
  },
  loadFormData(id) {
    request("/system/dept/" + id, "GET").then(res => {
      if (res.code == 200) {
        var form = res.data;
        var sta;
        if (form.status == "0") {
          sta = true;
        } else {
          sta = false;
        }
        this.setData({
          status: sta,
          form: form
        })
      }
    })
    console.log(this.data)
  },
  initValidate() {
    let rules = {
      deptName: {
        required: true
      },
      phone: {
        required: false,
        tel: true,
      },
      email: {
        required: false,
        email: true
      }
    }

    let message = {
      deptName: {
        required: '请输入部门名称'
      },
      phone: {
        tel: '请输入正确电话'
      },
      email: {
        required: "请输入正确邮箱"
      },
    }
    //实例化当前的验证规则和提示消息
    this.WxValidate = new WxValidate(rules, message);
  },
  deptPickerChange(e) {
    console.log(e);
    var data = this.data.form;
    data["parentId"] = this.data.dept[e.detail.value].deptId;
    this.setData({
      deptIndex: e.detail.value,
      form: data
    })
  },
  onChange(e) {
    var formData = this.data.form;
    formData.orderNum = e.detail.value;
    console.log(formData);
    this.setData({
      form: formData
    })
    console.log(this.data);
  },
  switchStatus() {
    if (this.data.status) {
      this.setData({
        status: false
      })
    } else {
      this.setData({
        status: true
      })
    }
    console.log(this.data.status);
  },
  formSubmit: function (e) {
    console.log(e);
    var formData = this.data.form;
    let params = e.detail.value;
    if (!this.WxValidate.checkForm(params)) {
      let error = this.WxValidate.errorList[0];
      switch (error.param) {
        case "deptName":
          //TODO
          break;
        case "phone":
          //TODO
          break;
        case "email":
          //TODO
          break;
      }
      console.log(error);
      wx.showToast({
        title: error.msg,
        icon: 'none',
        duration: 2000,
      })
      return false;
    }
    if(this.data.form.parentId == null){
      wx.showToast({
        title: "请选择上级部门",
        icon: 'none',
        duration: 2000,
      })
      return;
    }
    if (params.deptName != "") {
      formData["deptName"] = params.deptName;
    }
    if (params.phone != "") {
      formData["phone"] = params.phone;
    }
    if (params.leader != "") {
      formData["leader"] = params.leader;
    }
    if (params.email != "") {
      formData["email"] = params.email;
    }
    this.data.status == true ? formData["status"] = "0" : formData["status"] = "1";
    var status;
    if (formData.deptId != undefined) {
      status = "PUT";
    } else {
      status = "POST";
    }
    request("/system/dept", status, formData).then(res => {
      if (res.code == "200") {
        wx.showToast({
          title: '保存成功',
          icon: '',
          duration: 2000,
        })
        wx.navigateBack({
          delta: 1
        })
      } else {
        wx.showToast({
          title: res.msg+"",
          icon: 'none',
          duration: 2000,
        })
      }
    })
    console.log(formData);
  },
  leaderInput:function(e){
    var content = e.detail.value;
    var formData = this.data.form;
    formData.leader = content;
    this.setData({
      form: formData
    })
    console.log(this.data);
  },
  phoneInput:function(e){
    var content = e.detail.value;
    var formData = this.data.form;
    formData.phone = content;
    this.setData({
      form: formData
    })
    console.log(this.data);
  },
  emailInput:function(e){
    var content = e.detail.value;
    var formData = this.data.form;
    formData.email = content;
    this.setData({
      form: formData
    })
    console.log(this.data);
  },
  nameInput:function(e){
    var content = e.detail.value;
    var formData = this.data.form;
    formData.deptName = content;
    this.setData({
      form: formData
    })
    console.log(this.data);
  },
})